﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Xml;
using DCT.ViewModel;
using System;
using System.Windows.Media;

namespace DCT.View
{
    /// <summary>
    /// DeviceMapView.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceMapView : UserControl
    {
        #region Constructor

        public DeviceMapView()
        {
            InitializeComponent();

            this.DataContextChanged += new DependencyPropertyChangedEventHandler(DeviceMapView_DataContextChanged);
            this.Loaded += new RoutedEventHandler(DeviceMapView_Loaded);
            this.PreviewMouseMove += DeviceMapView_PreviewMouseMove;
        }

        #endregion //Constructor

        #region Properties

        public DeviceCanvas DeviceCanvas { get; set; }

        #endregion // Properties

        #region Fields

        DeviceMapViewModel _deviceMapViewModel;
        PackageCommand _packageCommand;

        #endregion //Fields

        #region Methods

        void DeviceMapView_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (_deviceMapViewModel.EditMode == EditModeState.DrawSwitch || _deviceMapViewModel.EditMode == EditModeState.DrawTrackSegment)
            {
                
                Point pt = e.GetPosition(deviceCanvasHostGrid);
                pt = new Point(Math.Round(pt.X, 1), Math.Round(pt.Y, 1));

                crossCursor.Margin = new Thickness(pt.X - 49, pt.Y - 49, 0, 0);

                ScaleTransform scaleTransform = DeviceCanvas.LayoutTransform as ScaleTransform;
                if (scaleTransform != null)
                {
                    crossCursor.PositionPoint = new Point(Math.Round(pt.X / scaleTransform.ScaleX, 2), Math.Round(pt.Y / scaleTransform.ScaleY, 2));
                }
            }
        }

        void DeviceMapView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext is DeviceMapViewModel)
            {
                _deviceMapViewModel = this.DataContext as DeviceMapViewModel;
            }
        }

        void DeviceMapView_Loaded(object sender, RoutedEventArgs e)
        {
            Window parentWindow = Window.GetWindow(this);
            if (parentWindow != null)
            {
                parentWindow.PreviewKeyDown += this.HandleWindowPreviewKeyDown;
                parentWindow.PreviewKeyUp += this.HandleWindowPreviewKeyUp;
            }
        }

        void HandleWindowPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (_deviceMapViewModel.IsActivated)
            {
                switch (e.Key)
                {
                    case Key.A:
                        if (Keyboard.Modifiers == ModifierKeys.Control)
                        {
                            if (_deviceMapViewModel.SelectAll_Can())
                            {
                                _deviceMapViewModel.SelectAll_Execute();
                                e.Handled = true;
                            }
                        }
                        break;
                    case Key.C:
                        if (Keyboard.Modifiers == ModifierKeys.Control)
                        {
                            if (_deviceMapViewModel.Copy_Can())
                            {
                                _deviceMapViewModel.Copy_Execute();
                                e.Handled = true;
                            }
                        }
                        break;
                    case Key.V:
                        if (Keyboard.Modifiers == ModifierKeys.Control)
                        {
                            if (_deviceMapViewModel.Paste_Can())
                            {
                                _deviceMapViewModel.Paste_Execute();
                                e.Handled = true;
                            }
                        }
                        break;
                    case Key.X:
                        if (Keyboard.Modifiers == ModifierKeys.Control)
                        {
                            if (_deviceMapViewModel.Cut_Can())
                            {
                                _deviceMapViewModel.Cut_Execute();
                                e.Handled = true;
                            }
                        }
                        break;
                    case Key.Y:
                        if (Keyboard.Modifiers == ModifierKeys.Control)
                        {
                            if (_deviceMapViewModel.Redo_Can())
                            {
                                _deviceMapViewModel.Redo_Execute();
                                e.Handled = true;
                            }
                        }
                        break;
                    case Key.Z:
                        if (Keyboard.Modifiers == ModifierKeys.Control)
                        {
                            if (_deviceMapViewModel.Undo_Can())
                            {
                                _deviceMapViewModel.Undo_Execute();
                                e.Handled = true;
                            }
                        }
                        break;
                    case Key.Delete:
                        if (_deviceMapViewModel.Delete_Can())
                        {
                            _deviceMapViewModel.Delete_Execute();
                            e.Handled = true;
                        }
                        break;
                    case Key.Left:
                        if (_packageCommand == null)
                        {
                            _packageCommand = new PackageCommand();
                            _deviceMapViewModel.UndoRedo.InsertInUnDoRedoForPackage(_packageCommand);
                        }
                        Point left = new Point(-1, 0);
                        if (_deviceMapViewModel.Move_Can(left))
                        {
                            _deviceMapViewModel.Move_Execute(left);
                            e.Handled = true;
                        }
                        break;
                    case Key.Up:
                        if (_packageCommand == null)
                        {
                            _packageCommand = new PackageCommand();
                            _deviceMapViewModel.UndoRedo.InsertInUnDoRedoForPackage(_packageCommand);
                        }
                        Point up = new Point(0, -1);
                        if (_deviceMapViewModel.Move_Can(up))
                        {
                            _deviceMapViewModel.Move_Execute(up);
                            e.Handled = true;
                        }
                        break;
                    case Key.Right:
                        if (_packageCommand == null)
                        {
                            _packageCommand = new PackageCommand();
                            _deviceMapViewModel.UndoRedo.InsertInUnDoRedoForPackage(_packageCommand);
                        }
                        Point right = new Point(1, 0);
                        if (_deviceMapViewModel.Move_Can(right))
                        {
                            _deviceMapViewModel.Move_Execute(right);
                            e.Handled = true;
                        }
                        break;
                    case Key.Down:
                        if (_packageCommand == null)
                        {
                            _packageCommand = new PackageCommand();
                            _deviceMapViewModel.UndoRedo.InsertInUnDoRedoForPackage(_packageCommand);
                        }
                        Point down = new Point(0, 1);
                        if (_deviceMapViewModel.Move_Can(down))
                        {
                            _deviceMapViewModel.Move_Execute(down);
                            e.Handled = true;
                        }
                        break;
                    case Key.F4:
                        if (Keyboard.Modifiers == ModifierKeys.Control)
                        {
                            if (_deviceMapViewModel.Close_Can())
                            {
                                _deviceMapViewModel.Close_Execute();
                                e.Handled = true;
                            }
                        }
                        break;
                    case Key.Escape:
                        if (_deviceMapViewModel.DrawTrackSegmentEnd_Can())
                        {
                            _deviceMapViewModel.DrawTrackSegmentEnd_Execute(false);
                            e.Handled = true;
                        }

                        if (_deviceMapViewModel.DrawSwitchNormal_Can())
                        {
                            _deviceMapViewModel.DrawSwitchNormal_Execute(false);
                            e.Handled = true;
                        }

                        if (_deviceMapViewModel.DrawSwitchReverse_Can())
                        {
                            _deviceMapViewModel.DrawSwitchReverse_Execute(false);
                            e.Handled = true;
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        void HandleWindowPreviewKeyUp(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Left:
                case Key.Up:
                case Key.Right:
                case Key.Down:
                    if (_packageCommand != null)
                    {
                        _deviceMapViewModel.UndoRedo.InsertInUnDoRedoForPackage(_packageCommand.AnotherPackageCommand);
                        _packageCommand = null;
                    }
                    break;
                default:
                    break;
            }

            e.Handled = true;
        }

        #endregion
    }
}
